package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;


@RestController
//@PropertySource({"classpath:application.properties"})
@PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true,encoding = "UTF-8" )
@RequestMapping("/lsj")
@Validated
public class UserController {



    @Value("${e3}")
    private String myName;
    @Autowired
    private UserMapper userMapper;

     /**
      *功能描述  测试校验
      * @author yuwc
      * @param  * @param id
      * @return
      *备注  http://localhost:9999/cc/lsj/getUserV?id=
      */
    @RequestMapping("getUserV")
    public String GetUserV(@NotBlank(message = "ID不能为空") String id){
        return "12132";
    }
    /**
     *功能描述:测试异常
     * @author yuwc
     * @param  * @param id 
     * @return 
     *备注  如果异常被处理，ControllerAdvice 不会捕获到异常 和aop 同理
     */
    @RequestMapping("/demoError")
    public String demoError(String id){
        int a= 0;
        try {
            a = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        return a+"";
    }
    /**
     *功能描述
     * @author yuwc
     * @date 2019/5/16 0016 上午 9:51
     * @param  * @param id
     * @return
     */
    @RequestMapping("getUser/{id}")
    @AfterReturning(returning = "ret",pointcut = "webLog()")
    public String GetUser(@PathVariable @NotNull(message = "ID不能为空") int id){
      return userMapper.getById(id).toString();
    }
  /**
   *功能描述
   * @author yuwc
   * @date 2019/5/16 0016  上午 9:46
   * @param  * @param
   * @return
   */
    @RequestMapping("who")
    public String who(){
        return myName;
    }

    @RequestMapping("cans")
    public String cans(User user){
        return user.getUserName();
    }
}